import random
import numpy as np
import pandas as pd

import sys
import pyhdfe
import pyblp
import statsmodels.api as sm



product_module_code = str(sys.argv[1])
path_head = str(sys.argv[2])

path = path_head + product_module_code  + '.csv'
product_data1 = pd.read_csv(path)


integration = pyblp.Integration('product', 9)

product_data1['xi'] = product_data1.delta - product_data1.alpha_2018[0]*product_data1.prices
beta_0 = np.mean(product_data1.xi)
product_data1['xi'] = product_data1['xi']-beta_0
product_data1["1"] = 1
product_data1["x1"] = 0
gamma_0_2018 = np.mean(product_data1.costs_2018)
gamma_0_2006 = np.mean(product_data1.costs_2006)
product_data1['omega_2018'] = product_data1['costs_2018']-gamma_0_2018
product_data1['omega_2006'] = product_data1['costs_2006']-gamma_0_2006




product_data1['firm_ids'] = product_data1['firm_ids_2018']
alpha = product_data1['alpha_2006'][0]
sigma = product_data1['sigma_2006'][0]
gamma = gamma_0_2018
product_data1['omega'] = product_data1['omega_2018']


sim = pyblp.Simulation(
    product_formulations = (
        pyblp.Formulation('1 + x1 + prices'),
                pyblp.Formulation('1'),
                pyblp.Formulation('1')
    ),
    product_data = product_data1,
    rho = sigma,
    sigma = np.array([0]),    
    beta = np.array([[beta_0,0.00001,alpha]]).T,
    gamma = np.array([gamma]), 
    xi = product_data1.xi,
    omega = product_data1.omega ,
    integration = integration  
)

sim_results = sim.replace_endogenous(prices = product_data1.prices, error_behavior = 'warn')
product_data1["prices_demand"] = sim_results.product_data.prices




product_data1['firm_ids'] = product_data1['firm_ids_2006']
alpha = product_data1['alpha_2018'][0]
sigma = product_data1['sigma_2018'][0]
gamma = gamma_0_2018
product_data1['omega'] = product_data1['omega_2018']


sim = pyblp.Simulation(
    product_formulations = (
        pyblp.Formulation('1 + x1 + prices'),
                pyblp.Formulation('1'),
                pyblp.Formulation('1')
    ),
    product_data = product_data1,
    rho = sigma,
    sigma = np.array([0]),    
    beta = np.array([[beta_0,0.00001,alpha]]).T,
    gamma = np.array([gamma]), 
    xi = product_data1.xi,
    omega = product_data1.omega ,
    integration = integration  
)

sim_results = sim.replace_endogenous(prices = product_data1.prices, error_behavior = 'warn')
product_data1["prices_ownership"] = sim_results.product_data.prices





product_data1['firm_ids'] = product_data1['firm_ids_2018']
alpha = product_data1['alpha_2018'][0]
sigma = product_data1['sigma_2018'][0]
gamma = gamma_0_2006
product_data1['omega'] = product_data1['omega_2006']


sim = pyblp.Simulation(
    product_formulations = (
        pyblp.Formulation('1 + x1 + prices'),
                pyblp.Formulation('1'),
                pyblp.Formulation('1')
    ),
    product_data = product_data1,
    rho = sigma,
    sigma = np.array([0]),    
    beta = np.array([[beta_0,0.00001,alpha]]).T,
    gamma = np.array([gamma]), 
    xi = product_data1.xi,
    omega = product_data1.omega ,
    integration = integration  
)

sim_results = sim.replace_endogenous(prices = product_data1.prices, error_behavior = 'warn')
product_data1["prices_costs"] = sim_results.product_data.prices



product_data1.to_csv(path)
